/*
 *  Copyright (c) 2023-2025 苏州棱镜七彩信息科技有限公司 (Suzhou PrismAurora Information Technology Co.,Ltd)
 *  sbom-generate is licensed under Mulan PSL v2.
 *  You can use this software according to the terms and conditions of the Mulan PSL v2.
 *  You may obtain a copy of Mulan PSL v2 at:
 *           http://license.coscl.org.cn/MulanPSL2
 *  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *  See the Mulan PSL v2 for more details.
 * */
package com.ljqc.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public final class FileUtils {

    private FileUtils() {

    }

    public static List<File> ls(File path) {
        List<File> result = new ArrayList<>();
        try {
            ls(path, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private static void ls(File item, List<File> result) {
        if (!item.exists()) {
            return;
        }

        if (Files.isRegularFile(item.toPath(), LinkOption.NOFOLLOW_LINKS)) {
            result.add(item);
            return;
        }

        if (Files.isDirectory(item.toPath(), LinkOption.NOFOLLOW_LINKS)) {
            File[] children = item.listFiles();
            if (children == null) {
                return;
            }

            for (File child : children) {
                if (Files.isRegularFile(child.toPath(), LinkOption.NOFOLLOW_LINKS)) {
                    result.add(child);
                } else if (Files.isDirectory(child.toPath(), LinkOption.NOFOLLOW_LINKS)) {
                    ls(child, result);
                }
            }
        }
    }

    public static String calculateMD5(File file) {
        try (InputStream is = Files.newInputStream(file.toPath())) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return getDigestStr(is, md);
        } catch (IOException | NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "00000000000000000000000000000000";
        }
    }

    private static String getDigestStr(InputStream is, MessageDigest md) throws IOException {
        byte[] buffer = new byte[8192];
        int read;
        while ((read = is.read(buffer)) > 0) {
            md.update(buffer, 0, read);
        }
        byte[] digest = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    public static String calculateSHA256(File file) {
        try (InputStream is = Files.newInputStream(file.toPath())) {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            return getDigestStr(is, md);
        } catch (IOException | NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "0000000000000000000000000000000000000000000000000000000000000000";
        }
    }
}
